import { getToken, setToken, removeToken, getUser,setUser,removeUser } from '@/utils/auth'
import api from '@/api/index.js'

const user = {
  state: {
    token:  getToken(),
    name: getUser("username"),
    realName:getUser("realname"),
    permissions: getUser("permissions"),
    normalPermissions:getUser("normalPermissions"),
    introduction: "",
    roles: [],
    setting: {
      articlePlatform: []
    }
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_INTRODUCTION: (state, introduction) => {
      state.introduction = introduction
    },
    SET_SETTING: (state, setting) => {
      state.setting = setting
    },
    
    SET_NAME: (state, name) => {
      state.name = name
    },
  
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },

    SET_REAlNAME: (state, realName) => {
      state.realName = realName
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    },
    SET_NORMALPERMISSIONS: (state, normalPermissions) => {
      state.normalPermissions = normalPermissions
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        api.post("/api/User/login", userInfo).then(rsp => {
          if (rsp != false) {
            commit('SET_TOKEN', rsp.data.access_token);
            commit('SET_NAME', rsp.data.loginUser.userName);
            commit('SET_REAlNAME', rsp.data.loginUser.realName);
            commit('SET_PERMISSIONS', rsp.data.loginUser.permissions);
            commit('SET_NORMALPERMISSIONS', rsp.data.loginUser.normalPermissions);
            setUser(rsp.data.loginUser);
            localStorage.setItem("refresh_token",rsp.data.refresh_token);
            setToken(rsp.data.access_token);
            resolve();
          }
        });
      });
    },


    // 退出
    LoginOut({ commit }) {
      return new Promise((resolve) => {
        let refresh_token = getUser("refresh_token");
        let data = { refresh_token: refresh_token }
        api.get("/api/User/loginOut", data).then(rsp => {
          if (rsp != false) {
            commit('SET_TOKEN', '')
            commit('SET_NAME', '')
            commit('SET_REAlNAME', '')
            commit('SET_PERMISSIONS', [])
            commit('SET_NORMALPERMISSIONS', []);
            localStorage.removeItem("refresh_token");
            removeToken();
            removeUser();
          }0
          resolve()
        });
      })
    },

    //刷新token
    RefreshToken({ commit }) {
      return new Promise((resolve) => {
        let refreshToken = localStorage.getItem("refresh_token");
        if (refreshToken != undefined) {
          let data = { refreshToken: refreshToken }
          api.get("/api/User/refreshToken", data).then(rsp => {
            if (rsp != false) {
              commit('SET_TOKEN', rsp.data.access_token);
              commit('SET_NAME', rsp.data.loginUser.userName);
              commit('SET_REAlNAME', rsp.data.loginUser.realName);
              commit('SET_PERMISSIONS', rsp.data.loginUser.permissions);
              commit('SET_NORMALPERMISSIONS', rsp.data.loginUser.normalPermissions);
              setUser(rsp.data.loginUser);
              localStorage.setItem("refresh_token",rsp.data.refresh_token);
              setToken(rsp.data.access_token);
            }
          });
        }
        resolve();
      });
    },


    // 动态修改权限
    // ChangeRoles({ commit }, role) {
    //   return new Promise(resolve => {
    //     commit('SET_TOKEN', role)
    //     setToken(role)
    //     getUserInfo(role).then(response => {
    //       const data = response.data
    //       commit('SET_ROLES', data.roles)
    //       commit('SET_NAME', data.name)
    //       commit('SET_AVATAR', data.avatar)
    //       commit('SET_INTRODUCTION', data.introduction)
    //       resolve()
    //     })
    //   })
    // }
  }
}

export default user
